var KOC = require("koc.common.utils");
var ExcelExport = require('excel-export');
var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");

module.exports = {
    Main: function (req, res, callback) {
        var _UserData = StateData.Value(req, res);
        if (!_UserData.IsLogin) {
            res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
            return;
        }
        //判断权限
        if (!_UserData.IsAdmin) {
            res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
            return;
        }
        res.render("statistics-manage/main", callback);
    },
    //用户统计
    Demographic_Manage: {
        /////////////////////////////
        //每日用户统计列表
        ////////////////////////////
        Day_User_List: function (req, res, callback) {
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            res.render("statistics-manage/demographics-manage/dayuserlist", callback);
        },
        /////////////////////////////
        //每日艺人统计列表
        ////////////////////////////
        Day_Anchor_List: function (req, res, callback) {
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            res.render("statistics-manage/demographics-manage/dayanchorlist", callback);
        },
        /////////////////////////////
        //每日艺人统计导出
        ////////////////////////////
        Day_Anchor_List_ExportExcel: function (req, res) {
            req.params.iDisplayLength = 100000;//默认十万条
            var reqModel = CommonUtils.Common.AjaxPage.Request(req);
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            var reqParm = new DALFactory.Report.Page.AnchorDayStatistics_Parm();
            if (reqModel.Filter_Value.length > 0) {
                switch (reqModel.Filter_Type) {
                    case "User_ID":
                        reqParm.User_ID = reqModel.Filter_Value;
                        break;
                    case "Family_ID":
                        reqParm.Family_ID = reqModel.Filter_Value;
                        break;
                    default:
                        reqParm.FuzzyQuery = reqModel.Filter_Value;
                        break;
                }
            }
            reqParm.User_ID = reqParm.User_ID ? reqParm.User_ID : ( req.params.F_User_ID ? KOC.StringUtils.ToInt(req.params.F_User_ID): null);
            reqParm.Family_ID = req.params.F_Family_ID ? KOC.StringUtils.ToInt(req.params.F_Family_ID) : (reqParm.Family_ID ? reqParm.Family_ID : null);
            reqParm.Report_Day = KOC.StringUtils.ToInt(req.params.F_Create_Date);
            reqParm.Create_Date_Between = req.params.F_StartDate ? KOC.Moment(req.params.F_StartDate)._d : null;
            reqParm.Create_Date_And = req.params.F_EndDate ? KOC.Moment(req.params.F_EndDate)._d : null;
            if (req.params.F_Create_Date_Between && req.params.F_Create_Date_And) {
                reqParm.Create_Date_Between = KOC.Moment(req.params.F_Create_Date_Between)._d;
                reqParm.Create_Date_And = KOC.Moment(req.params.F_Create_Date_And)._d;
            }
            //排序
            switch (reqModel.Sort_Col) {
                case "User_ID":
                case "Report_Day":
                    break;
                default:
                    reqModel.Sort_Col = "`Report_Day`";
                    break;
            }
            DALFactory.Report.Page.AnchorDayStatistics(reqParm, reqModel, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode(retValue.message + "(" + retValue.errorCode + ")", reqModel.Echo));
                    return;
                }
                var conf = {};
                conf.cols = [
                    {caption: '日期', type: 'string'},
                    {caption: '用户ID', type: 'number'},
                    {caption: '公会ID', type: 'number'},
                    {caption: '艺人昵称', type: 'string'},
                    {caption: '真实姓名', type: 'string'},
                    {caption: '累计消费金币', type: 'number'},
                    {caption: '开播次数', type: 'number'},
                    {caption: '直播时长', type: 'number'},
                    {caption: '直播有效时长', type: 'number'},
                    {caption: '送礼总次数', type: 'number'},
                    {caption: '送礼总数量', type: 'number'},
                    {caption: '送礼总金币', type: 'number'},
                    {caption: '奖池发放次数', type: 'number'},
                    {caption: '奖池发放金币', type: 'number'},
                    {caption: '守护次数', type: 'number'},
                    {caption: '守护总金币', type: 'number'},
                    {caption: '喇叭总次数', type: 'number'},
                    {caption: '喇叭总金币', type: 'number'}
                ];
                conf.rows = [];
                KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                    conf.rows.push([
                        KOC.Moment(ThisValue.Report_Day).format('YYYY-MM-DD'),
                        ThisValue.User_ID,
                        ThisValue.Family_ID,
                        ThisValue.NickName,
                        ThisValue.RealName,
                        ThisValue.Coin,
                        ThisValue.Live_Count,
                        ThisValue.Live_Duration,
                        ThisValue.Live_Duration_Efftive,
                        ThisValue.Gift_Count,
                        ThisValue.Gift_Num,
                        ThisValue.Gift_Coin,
                        ThisValue.Gift_Pond_Count,
                        ThisValue.Gift_Pond_Coin,
                        ThisValue.Guardian_Count,
                        ThisValue.Guardian_Coin,
                        ThisValue.Horn_Count,
                        ThisValue.Horn_Coin
                    ]);
                });
                var result = ExcelExport.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "ExportExcel.xlsx");
                res.end(result, 'binary');
            });
        },
        /////////////////////////////
        //每日平台统计列表
        ////////////////////////////
        Platform_Day_List: function (req, res, callback) {
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            res.render("statistics-manage/demographics-manage/platformdailylist", callback);
        },
        /////////////////////////////
        //每日平台统计导出
        ////////////////////////////
        Platform_Day_ExportExcel: function (req, res) {
            req.params.iDisplayLength = 100000;//默认十万条
            var reqModel = CommonUtils.Common.AjaxPage.Request(req);
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            var reqParm = new DALFactory.Report.Page.PlatformDayStatistics_Parm();
            if (reqModel.Filter_Value.length > 0) {
                switch (reqModel.Filter_Type) {
                    case "Report_Day":
                        reqParm.Report_Day = reqModel.Filter_Value;
                        break;
                    default:
                        reqParm.FuzzyQuery = reqModel.Filter_Value;
                        break;
                }
            }
            reqParm.Report_Day = req.params.F_Create_Date ? KOC.StringUtils.ToInt(req.params.F_Create_Date) : (reqParm.Report_Day ? reqParm.Report_Day : null);
            reqParm.Create_Date_Between = req.params.F_StartDate ? req.params.F_StartDate : null;
            reqParm.Create_Date_And = req.params.F_EndDate ? req.params.F_EndDate : null;
            if (req.params.F_Create_Date_Between && req.params.F_Create_Date_And) {
                reqParm.Create_Date_Between = KOC.Moment(req.params.F_Create_Date_Between)._d;
                reqParm.Create_Date_And = KOC.Moment(req.params.F_Create_Date_And)._d;
            }
            //排序
            switch (reqModel.Sort_Col) {
                case "Report_Day":
                    break;
                default:
                    reqModel.Sort_Col = "`Report_Day`";
                    break;
            }
            DALFactory.Report.Page.PlatformDayStatistics(reqParm, reqModel, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode(retValue.message + "(" + retValue.errorCode + ")", reqModel.Echo));
                    return;
                }
                var conf = {};
                conf.cols = [
                    {caption: '日期(天)', type: 'string'},
                    {caption: '用户总数量', type: 'number'},
                    {caption: '用户注册数量', type: 'number'},
                    {caption: '用户登录数量', type: 'number'},
                    {caption: 'VIP用户数量', type: 'number'},
                    {caption: '消费用户数', type: 'number'},
                    {caption: '用户金币余额', type: 'number'},
                    {caption: '用户金豆余额', type: 'number'},
                    {caption: '艺人数量', type: 'number'},
                    {caption: '艺人注册数量', type: 'number'},
                    {caption: '艺人登录数量', type: 'number'},
                    {caption: '开播艺人数量', type: 'number'},
                    {caption: '公会数量', type: 'number'},
                    {caption: '新建公会数量', type: 'number'},
                    {caption: '公会金豆余额', type: 'number'},
                    {caption: '开播次数', type: 'number'},
                    {caption: '直播时长', type: 'number'},
                    {caption: '充值数量', type: 'number'},
                    {caption: '充值次数', type: 'number'},
                    {caption: '充值金额(RMB)', type: 'number'},
                    {caption: '充值金币', type: 'number'},
                    {caption: '充值手续费', type: 'number'},
                    {caption: '送礼物总次数(包含包裹)', type: 'number'},
                    {caption: '送礼物总次数(包裹)', type: 'number'},
                    {caption: '送礼物总数量(包含包裹)', type: 'number'},
                    {caption: '送礼物总数量(包裹)', type: 'number'},
                    {caption: '送礼物总金币(包含包裹)', type: 'number'},
                    {caption: '送礼物总金币(包裹)', type: 'number'},
                    {caption: '送礼物总金豆(艺人收入)(包含包裹)', type: 'number'},
                    {caption: '送礼物总金豆(艺人收入)(包裹)', type: 'number'},
                    {caption: '送礼物总金豆(公会收入)(包含包裹)', type: 'number'},
                    {caption: '送礼物总金豆(公会收入)(包裹)', type: 'number'},
                    {caption: '送礼物总金豆(平台收入)(包含包裹)', type: 'number'},
                    {caption: '送礼物总金豆(平台收入)(包裹)', type: 'number'},
                    {caption: '奖池发放总次数', type: 'number'},
                    {caption: '奖池发放总金币', type: 'number'},
                    {caption: '幸运礼物总次数', type: 'number'},
                    {caption: '幸运礼物返金币总数', type: 'number'},
                    {caption: '幸运礼物花费金币总数', type: 'number'},
                    {caption: '幸运礼物总个数', type: 'number'},
                    {caption: '守护总次数', type: 'number'},
                    {caption: '守护总金币', type: 'number'},
                    {caption: '守护总金豆(艺人)', type: 'number'},
                    {caption: '守护总金豆(公会)', type: 'number'},
                    {caption: '守护总金豆(平台)', type: 'number'},
                    {caption: '喇叭总次数(包含包裹)', type: 'number'},
                    {caption: '喇叭总次数(包裹)', type: 'number'},
                    {caption: '喇叭总金币(包含包裹)', type: 'number'},
                    {caption: '喇叭总金币(包裹)', type: 'number'},
                    {caption: '喇叭总金豆(艺人收入)(包含包裹)', type: 'number'},
                    {caption: '喇叭总金豆(艺人收入)(包裹)', type: 'number'},
                    {caption: '喇叭总金豆(公会收入)(包含包裹)', type: 'number'},
                    {caption: '喇叭总金豆(公会收入)(包裹)', type: 'number'},
                    {caption: '喇叭总金豆(平台收入)(包含包裹)', type: 'number'},
                    {caption: '喇叭总金豆(平台收入)(包裹)', type: 'number'},
                    {caption: 'VIP用户数量', type: 'number'},
                    {caption: 'VIP购买次数', type: 'number'},
                    {caption: 'VIP购买金额', type: 'number'},
                    {caption: '座骑购买数量', type: 'number'},
                    {caption: '座骑购买次数', type: 'number'},
                    {caption: '座骑购买金额', type: 'number'},
                    {caption: '转盘金币总数', type: 'number'},
                    {caption: '转盘铁金币总数', type: 'number'},
                    {caption: '转盘铜金币总数', type: 'number'},
                    {caption: '转盘银金币总数', type: 'number'},
                    {caption: '转盘金金币总数', type: 'number'},
                    {caption: '转盘抽奖总次数', type: 'number'},
                    {caption: '转盘铁抽奖总次数', type: 'number'},
                    {caption: '转盘铜抽奖总次数', type: 'number'},
                    {caption: '转盘银抽奖总次数', type: 'number'},
                    {caption: '转盘金抽奖总次数', type: 'number'},
                    {caption: '用户每日总消费', type: 'number'}
                ];
                conf.rows = [];
                KOC.Lodash.forEach(retValue.returnObject, function (ThisValue) {
                    conf.rows.push([
                        KOC.Moment(ThisValue.Report_Day).format('YYYY-MM-DD'),
                        ThisValue.User_Count,
                        ThisValue.User_Count_SignUp,
                        ThisValue.User_Count_SignIn,
                        ThisValue.User_Count_VIP,
                        ThisValue.User_Count_Consumption,
                        ThisValue.User_Coin,
                        ThisValue.User_Beans,
                        ThisValue.Anchor_Count,
                        ThisValue.Anchor_Count_SignUp,
                        ThisValue.Anchor_Count_SignIn,
                        ThisValue.Anchor_Count_Live,
                        ThisValue.Family_Count,
                        ThisValue.Family_Count_SignUp,
                        ThisValue.Family_Beans,
                        ThisValue.Live_Count,
                        ThisValue.Live_Duration,
                        ThisValue.Recharge_User,
                        ThisValue.Recharge_Count,
                        ThisValue.Recharge_Money,
                        ThisValue.Recharge_Coin,
                        ThisValue.Recharge_Poundage,
                        ThisValue.Gift_Count,
                        ThisValue.Gift_Count_Package,
                        ThisValue.Gift_Num,
                        ThisValue.Gift_Num_Package,
                        ThisValue.Gift_Coin,
                        ThisValue.Gift_Coin_Package,
                        ThisValue.Gift_Beans_Anchor,
                        ThisValue.Gift_Beans_Anchor_Package,
                        ThisValue.Gift_Beans_Family,
                        ThisValue.Gift_Beans_Family_Package,
                        ThisValue.Gift_Beans_Platform,
                        ThisValue.Gift_Beans_Platform_Package,
                        ThisValue.Gift_Pond_Count,
                        ThisValue.Gift_Pond_Coin,
                        ThisValue.Gift_Count_Lucky,
                        ThisValue.Gift_Lucky_Coin,
                        ThisValue.Gift_Coin_Lucky,
                        ThisValue.Gift_Num_Lucky,
                        ThisValue.Guardian_Count,
                        ThisValue.Guardian_Coin,
                        ThisValue.Guardian_Beans_Anchor,
                        ThisValue.Guardian_Beans_Family,
                        ThisValue.Guardian_Beans_Platform,
                        ThisValue.Horn_Count,
                        ThisValue.Horn_Count_Package,
                        ThisValue.Horn_Coin,
                        ThisValue.Horn_Coin_Package,
                        ThisValue.Horn_Beans_Anchor,
                        ThisValue.Horn_Beans_Anchor_Package,
                        ThisValue.Horn_Beans_Family,
                        ThisValue.Horn_Beans_Family_Package,
                        ThisValue.Horn_Beans_Platform,
                        ThisValue.Horn_Beans_Platform_Package,
                        ThisValue.VIP_User,
                        ThisValue.VIP_Count,
                        ThisValue.VIP_Coin,
                        ThisValue.Mounts_User,
                        ThisValue.Mounts_Count,
                        ThisValue.Mounts_Coin,
                        ThisValue.Lottery_Coin,
                        ThisValue.Lottery_CategoryIron_Coin,
                        ThisValue.Lottery_CategoryCopper_Coin,
                        ThisValue.Lottery_CategorySilver_Coin,
                        ThisValue.Lottery_CategoryGold_Coin,
                        ThisValue.Lottery_Count,
                        ThisValue.Lottery_CategoryIron_Count,
                        ThisValue.Lottery_CategoryCopper_Count,
                        ThisValue.Lottery_CategorySilver_Count,
                        ThisValue.Lottery_CategoryGold_Count,
                        ThisValue.User_Tonsume_Total = ThisValue.Gift_Coin + ThisValue.Guardian_Coin + (ThisValue.Horn_Coin - ThisValue.Horn_Coin_Package) + ThisValue.VIP_Coin + ThisValue.Mounts_Coin + ThisValue.Lottery_Coin
                    ]);
                });
                var result = ExcelExport.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "PlatformDailyExcel.xlsx");
                res.end(result, 'binary');
            });
        }
    },
    //渠道管理
    Channel_Manage: {
        /////////////////////////////
        //每日渠道统计列表
        ////////////////////////////
        List: function (req, res, callback) {
            var _UserData = StateData.Value(req, res);
            if (!_UserData.IsLogin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("请登录后使用此功能", reqModel.Echo));
                return;
            }
            //判断权限
            if (!_UserData.IsAdmin) {
                res.jsonp(CommonUtils.Common.AjaxPage.ErrorCode("没有权限", reqModel.Echo));
                return;
            }
            var _PageData = {
                flag: 0,
                message: ''
            };
            DALFactory.Report.Channel_Day_.Info_ChannelID(function (retValue) {
                if (retValue.hasError) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else if (!retValue.returnObject) {
                    _PageData.flag = retValue.errorCode;
                    _PageData.message = retValue.message;
                } else {
                    _PageData.data = retValue.returnObject;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render("statistics-manage/channel-manage/list", callback);
            });
        }
    }

};